import { SET_LOGIN, SetLoginAction, UserInfo } from './actions';

export interface LoginState {
  isLogin: boolean;
  userInfo: UserInfo;
}

export interface AppState {
  Language: string;
  Theme: string;
}

export interface RootState {
  loginState: LoginState;
  appState: AppState;
}

const loginState: LoginState = {
  isLogin: false,
  userInfo: {
    name: '',
    avatar: '',
    token: '',
  },
};

const appState: AppState = {
  Language: 'zh',
  Theme: 'light',
};

const initialState: RootState = {
  loginState,
  appState,
};

export const rootReducer = (
  state = initialState,
  action: SetLoginAction
): RootState => {
  switch (action.type) {
    case SET_LOGIN:
      return {
        ...state,
        loginState: {
          isLogin: action.payload.isLogin,
          userInfo: action.payload.userInfo,
        },
      };
    default:
      return state;
  }
};
